﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Jemgine.Engine.Scripts
{
    [Category("Physics"), Node, Description("Move an entity to a waypoint.")]
   public class MoveEntity: Data.ScriptNode
    {
        public float Speed { get; set; }

       public MoveEntity()
       {
           AddInput("Fire", null);
           AddReference("Entity", typeof(Data.EntityRecord));
           AddReference("Waypoint", typeof(Data.Waypoint));
           AddOutput("Began", null);
           AddOutput("Arrived", null);

           Speed = 16;
       }

       public override void FireTerminal(int InputID, Data.ScriptPayload Payload)
       {
           PhysicsModule Module = Sim.GetModule<PhysicsModule>();
           Module.AddMover(this,Payload);
       }

      
    }

    [Category("Physics"), Node]
    public class TeleportEntity : Data.ScriptNode
    {
        public TeleportEntity()
        {
            AddInput("Fire", null);
            AddReference("Entity", typeof(Data.EntityRecord));
            AddReference("Waypoint", typeof(Data.Waypoint));
            AddOutput("Relay", null);
        }

        public override void FireTerminal(int InputID, Data.ScriptPayload Payload)
        {
            var Entity = ReferenceTerminals[0].GetTarget(Payload) as Data.EntityRecord;
            var Pos = ReferenceTerminals[1].GetTarget(Payload) as Data.Waypoint;
            if (Entity != null && Pos != null)
            {
                Entity.Position = Pos.Position;
                var Physics = Entity.GetComponent<Physics>();
                if (Physics != null && Physics.PhysicsBody != null) Physics.PhysicsBody.Position = Pos.Position;
            }
            OutputTerminals[0].Fire(Payload);
        }
    }
}
